﻿using System.Linq;
using System.Collections.Generic;

using Cart;
using BusinessObjects;
using ActionService.DataTransferObjects;

namespace ActionService.DataTransferObjectMapper
{
    /// <summary>
    /// Maps DTOs (Data Transfer Objects) to BOs (Business Objects) and vice versa.
    /// </summary>
    public static class Mapper
    {
        /// <summary>
        /// Transforms list of category BOs list of category DTOs.
        /// </summary>
        /// <param name="categories">List of categories BOs.</param>
        /// <returns>List of category DTOs.</returns>
        public static IList<CategoryDto> ToDataTransferObjects(IEnumerable<Category> categories)
        {
            if (categories == null) return null;
            return categories.Select(c => ToDataTransferObject(c)).ToList();
        }

        /// <summary>
        /// Transforms category BO to category DTO.
        /// </summary>
        /// <param name="category">Category BO.</param>
        /// <returns>Category DTO.</returns>
        public static CategoryDto ToDataTransferObject(Category category)
        {
            if (category == null) return null;

            return new CategoryDto
            {
                CategoryId = category.CategoryId,
                TrafficId = category.TrafficId,
                SupplierId = category.SupplierId,
                Date = category.Date,
                Description = category.Description,
                FirstPoint = category.FirstPoint,
                LastPoint = category.LastPoint,
                StartTime = category.StartTime,
                EndTime = category.EndTime,
                Version = category.Version,
                Status = category.Status,
            };
        }

        public static Category FromDataTransferObject(CategoryDto category)
        {
            if (category == null) return null;

            return new Category
            {
                CategoryId = category.CategoryId,
                TrafficId = category.TrafficId,
                SupplierId = category.SupplierId,
                Date = category.Date,
                Description = category.Description,
                FirstPoint = category.FirstPoint,
                LastPoint = category.LastPoint,
                StartTime = category.StartTime,
                EndTime = category.EndTime,
                Version = category.Version,
                Status = category.Status,
            };
        }


        /// <summary>
        /// Transforms list of Product BOs to list of Product DTOs.
        /// </summary>
        /// <param name="products">List of Product BOs.</param>
        /// <returns>List of Product DTOs.</returns>
        public static IList<CountryDto> ToDataTransferObjects(IEnumerable<Country> countries)
        {
            if (countries == null) return null;
            return countries.Select(p => ToDataTransferObject(p)).ToList();
        }

        /// <summary>
        /// Transforms countries BO to countries DTO.
        /// </summary>
        /// <param name="countries">Product BO.</param>
        /// <returns>countries DTO.</returns>
        public static CountryDto ToDataTransferObject(Country country)
        {
            if (country == null) return null;

            return new CountryDto
            {
                CountryId = country.CountryId,
                CountryName = country.CountryName,
            };
        }

        public static Country FromDataTransferObject(CountryDto country)
        {
            if (country == null) return null;

            return new Country
            {
                CountryId = country.CountryId,
                CountryName = country.CountryName,
            };
        }

        /// <summary>
        /// Transforms list of Customer BOs to list of Customer DTOs.
        /// </summary>
        /// <param name="customers">List of Customer BOs.</param>
        /// <returns>List of Customer DTOs.</returns>
        public static IList<CustomerDto> ToDataTransferObjects(IEnumerable<Customer> customers)
        {
            if (customers == null) return null;
            return customers.Select(c => ToDataTransferObject(c)).ToList();
        }

        /// <summary>
        /// Transforms Customer BO to Customer DTO.
        /// </summary>
        /// <param name="customer">Customer BO.</param>
        /// <returns>Customer DTO.</returns>
        public static CustomerDto ToDataTransferObject(Customer customer)
        {
            if (customer == null) return null;

            return new CustomerDto
            {
                CustomerId = customer.CustomerId,
                CompanyName = customer.CompanyName,
                Name = customer.Name,
                City = customer.City,
                Country = customer.Country,
                UserName = customer.UserName,
                Password = customer.Password,
                Email = customer.Email,
                Telephone = customer.Telephone,
                MobilePhone = customer.MobilePhone,
                YahooId = customer.YahooId,
                SkypeId = customer.SkypeId,
                GoogleId = customer.GoogleId,
                FacebookId = customer.FacebookId,
                Status = customer.Status,
            };
        }

        /// <summary>
        /// Transfers Customer DTO to Customer BO.
        /// </summary>
        /// <param name="c">Customer DTO.</param>
        /// <returns>Customer BO.</returns>
        public static Customer FromDataTransferObject(CustomerDto customer)
        {
            if (customer == null) return null;

            return new Customer
            {
                CustomerId = customer.CustomerId,
                CompanyName = customer.CompanyName,
                Name = customer.Name,
                City = customer.City,
                Country = customer.Country,
                UserName = customer.UserName,
                Password = customer.Password,
                Email = customer.Email,
                Telephone = customer.Telephone,
                MobilePhone = customer.MobilePhone,
                YahooId = customer.YahooId,
                SkypeId = customer.SkypeId,
                GoogleId = customer.GoogleId,
                FacebookId = customer.FacebookId,
                Status = customer.Status,
            };
        }


        /// <summary>
        /// Transforms list of Customer BOs to list of Customer DTOs.
        /// </summary>
        /// <param name="customers">List of Customer BOs.</param>
        /// <returns>List of Customer DTOs.</returns>
        public static IList<EmployeeDto> ToDataTransferObjects(IEnumerable<Employee> employees)
        {
            if (employees == null) return null;
            return employees.Select(e => ToDataTransferObject(e)).ToList();
        }

        /// <summary>
        /// Transforms Employee BO to Employee DTO.
        /// </summary>
        /// <param name="customer">Employee BO.</param>
        /// <returns>Employee DTO.</returns>
        public static EmployeeDto ToDataTransferObject(Employee employee)
        {
            if (employee == null) return null;

            return new EmployeeDto
            {
                EmployeeId = employee.EmployeeId,
                SupplierId = employee.SupplierId,
                Name = employee.Name,
                Picture = employee.Picture,
                Sex = employee.Sex,
                YahooId = employee.YahooId,
                SkypeId = employee.SkypeId,
                GoogleId = employee.GoogleId,
                FacebookId = employee.FacebookId,
                Telephone = employee.Telephone,
                MobilePhone = employee.MobilePhone,
                Version = employee.Version,
                Status = employee.Status,
            };
        }

        public static Employee FromDataTransferObject(EmployeeDto employee)
        {
            if (employee == null) return null;

            return new Employee
            {
                EmployeeId = employee.EmployeeId,
                SupplierId = employee.SupplierId,
                Name = employee.Name,
                Picture = employee.Picture,
                Sex = employee.Sex,
                YahooId = employee.YahooId,
                SkypeId = employee.SkypeId,
                GoogleId = employee.GoogleId,
                FacebookId = employee.FacebookId,
                Telephone = employee.Telephone,
                MobilePhone = employee.MobilePhone,
                Version = employee.Version,
                Status = employee.Status,
            };
        }

        /// <summary>
        /// Transforms list of Order BOs to list of Order DTOs.
        /// </summary>
        /// <param name="orders">List of Order BOs.</param>
        /// <returns>List of Order DTOs.</returns>
        public static OrderDto[] ToDataTransferObjects(IEnumerable<Order> orders)
        {
            if (orders == null) return null;
            return orders.Select(o => ToDataTransferObject(o)).ToArray();
        }

        /// <summary>
        /// Transfers Order BO to Order DTO.
        /// </summary>
        /// <param name="order">Order BO.</param>
        /// <returns>Order DTO.</returns>
        public static OrderDto ToDataTransferObject(Order order)
        {
            if (order == null) return null;

            return new OrderDto
            {
                OrderDetails = order.OrderDetails,
                OrderId = order.OrderId,
                CustomerId = order.CustomerId,
                SupplierId = order.SupplierId,
                OrderDate = order.OrderDate,                      
                RequiredDate = order.RequiredDate,
                //ShippedDate = order.ShippedDate,
                Freight = order.Freight,
                Version = order.Version,
                Status = order.Status,
            };
        }

        public static Order FromDataTransferObject(OrderDto order)
        {
            if (order == null) return null;

            return new Order
            {
                OrderDetails = order.OrderDetails,
                OrderId = order.OrderId,
                CustomerId = order.CustomerId,
                SupplierId = order.SupplierId,
                OrderDate = order.OrderDate,
                RequiredDate = order.RequiredDate,
                //ShippedDate = order.ShippedDate,
                Freight = order.Freight,
                Version = order.Version,
                Status = order.Status,
            };
        }

        /// <summary>
        /// Transfers list of OrderDetail BOs to list of OrderDetail DTOs.
        /// </summary>
        /// <param name="orderDetails">List of OrderDetail BOs.</param>
        /// <returns>List of OrderDetail DTOs.</returns>
        public static OrderDetailDto[] ToDataTransferObjects(IEnumerable<OrderDetail> orderDetails)
        {
            if (orderDetails == null) return null;
            return orderDetails.Select(o => ToDataTransferObject(o)).ToArray();
        }

        /// <summary>
        /// Transfers OrderDetail BO to OrderDetail DTO.
        /// </summary>
        /// <param name="orderDetail">OrderDetail BO.</param>
        /// <returns>OrderDetail DTO.</returns>
        public static OrderDetailDto ToDataTransferObject(OrderDetail orderDetail)
        {
            if (orderDetail == null) return null;

            return new OrderDetailDto
            {
                OrderId = orderDetail.OrderId,
                CategoryId = orderDetail.CategoryId,
                ProductId = orderDetail.ProductId,
                UnitPrice = orderDetail.UnitPrice,
                Quantity = orderDetail.Quantity,
                Discount = orderDetail.Discount,
                Version = orderDetail.Version,
                Status = orderDetail.Status,
            };
        }

        public static OrderDetail FromDataTransferObject(OrderDetailDto orderDetail)
        {
            if (orderDetail == null) return null;

            return new OrderDetail
            {
                OrderId = orderDetail.OrderId,
                CategoryId = orderDetail.CategoryId,
                ProductId = orderDetail.ProductId,
                UnitPrice = orderDetail.UnitPrice,
                Quantity = orderDetail.Quantity,
                Discount = orderDetail.Discount,
                Version = orderDetail.Version,
                Status = orderDetail.Status,
            };
        }


        /// <summary>
        /// Transfers list of Point BOs to list of Point DTOs.
        /// </summary>
        /// <param name="orderDetails">List of PointDetail BOs.</param>
        /// <returns>List of Point DTOs.</returns>
        public static PointDto[] ToDataTransferObjects(IEnumerable<Point> points)
        {
            if (points == null) return null;
            return points.Select(p => ToDataTransferObject(p)).ToArray();
        }

        /// <summary>
        /// Transfers OrderDetail BO to OrderDetail DTO.
        /// </summary>
        /// <param name="orderDetail">OrderDetail BO.</param>
        /// <returns>OrderDetail DTO.</returns>
        public static PointDto ToDataTransferObject(Point point)
        {
            if (point == null) return null;

            return new PointDto
            {
                PointId = point.PointId,
                ProvinceId = point.ProvinceId,
                Description = point.Description,
            };
        }

        public static Point FromDataTransferObject(PointDto point)
        {
            if (point == null) return null;

            return new Point
            {
                PointId = point.PointId,
                ProvinceId = point.ProvinceId,
                Description = point.Description,
            };
        }

        /// <summary>
        /// Transforms list of Product BOs to list of Product DTOs.
        /// </summary>
        /// <param name="products">List of Product BOs.</param>
        /// <returns>List of Product DTOs.</returns>
        public static IList<ProductDto> ToDataTransferObjects(IEnumerable<Product> products)
        {
            if (products == null) return null;
            return products.Select(p => ToDataTransferObject(p)).ToList();
        }

        /// <summary>
        /// Transforms Product BO to Product DTO.
        /// </summary>
        /// <param name="product">Product BO.</param>
        /// <returns>Product DTO.</returns>
        public static ProductDto ToDataTransferObject(Product product)
        {
            if (product == null) return null;

            return new ProductDto
            {
                ProductId = product.ProductId,
                CategoryId = product.CategoryId,
                ChairNumber = product.ChairNumber,
                UnitPrice = product.UnitPrice,
                Description = product.Description,
                Version = product.Version,
                Status = product.Status,
            };
        }

        public static Product FromDataTransferObject(ProductDto product)
        {
            if (product == null) return null;

            return new Product
            {
                ProductId = product.ProductId,
                CategoryId = product.CategoryId,
                ChairNumber = product.ChairNumber,
                UnitPrice = product.UnitPrice,
                Description = product.Description,
                Version = product.Version,
                Status = product.Status,
            };
        }

        /// <summary>
        /// Transfers list of Province BOs to list of Province DTOs.
        /// </summary>
        /// <param name="Province">List of Province BOs.</param>
        /// <returns>List of Province DTOs.</returns>
        public static ProvinceDto[] ToDataTransferObjects(IEnumerable<Province> provinces)
        {
            if (provinces == null) return null;
            return provinces.Select(p => ToDataTransferObject(p)).ToArray();
        }

        /// <summary>
        /// Transfers Province BO to OrderDetail DTO.
        /// </summary>
        /// <param name="Province">Province BO.</param>
        /// <returns>Province DTO.</returns>
        public static ProvinceDto ToDataTransferObject(Province province)
        {
            if (province == null) return null;

            return new ProvinceDto
            {
                ProvinceId = province.ProvinceId,
                CountryId = province.CountryId,
                ProvinceName = province.ProvinceName,
            };
        }

        public static Province FromDataTransferObject(ProvinceDto province)
        {
            if (province == null) return null;

            return new Province
            {
                ProvinceId = province.ProvinceId,
                CountryId = province.CountryId,
                ProvinceName = province.ProvinceName,
            };
        }

        /// <summary>
        /// Transfers list of Service BOs to list of Service DTOs.
        /// </summary>
        /// <param name="Service">List of Service BOs.</param>
        /// <returns>List of Service DTOs.</returns>
        public static ServiceDto[] ToDataTransferObjects(IEnumerable<Service> services)
        {
            if (services == null) return null;
            return services.Select(s => ToDataTransferObject(s)).ToArray();
        }

        /// <summary>
        /// Transfers Service BO to Service DTO.
        /// </summary>
        /// <param name="Service">Service BO.</param>
        /// <returns>Service DTO.</returns>
        public static ServiceDto ToDataTransferObject(Service service)
        {
            if (service == null) return null;

            return new ServiceDto
            {
                ServiceId = service.ServiceId,
                Description = service.Description,                
            };
        }

        public static Service FromDataTransferObject(ServiceDto service)
        {
            if (service == null) return null;

            return new Service
            {
                ServiceId = service.ServiceId,
                Description = service.Description,
            };
        }

        /// <summary>
        /// Transforms list of Customer BOs to list of Customer DTOs.
        /// </summary>
        /// <param name="customers">List of Customer BOs.</param>
        /// <returns>List of Customer DTOs.</returns>
        public static IList<SupplierDto> ToDataTransferObjects(IEnumerable<Supplier> suppliers)
        {
            if (suppliers == null) return null;
            return suppliers.Select(c => ToDataTransferObject(c)).ToList();
        }

        /// <summary>
        /// Transforms Customer BO to Customer DTO.
        /// </summary>
        /// <param name="customer">Customer BO.</param>
        /// <returns>Customer DTO.</returns>
        public static SupplierDto ToDataTransferObject(Supplier supplier)
        {
            if (supplier == null) return null;

            return new SupplierDto
            {
                SupplierId = supplier.SupplierId,
                CompanyName = supplier.CompanyName,
                Email = supplier.Email,
                CompanyPhone = supplier.CompanyPhone,
                Mobilephone = supplier.Mobilephone,
                YahooId = supplier.YahooId,
                SkypeId = supplier.SkypeId,
                GoogleId = supplier.GoogleId,
                FacebookId = supplier.FacebookId,
                UserName = supplier.UserName,
                Password = supplier.Password,
                Address = supplier.Address,
                Description = supplier.Description,
                Status = supplier.Status,
                Version = supplier.Version
            };
        }

        public static Supplier FromDataTransferObject(SupplierDto supplier)
        {
            if (supplier == null) return null;

            return new Supplier
            {
                SupplierId = supplier.SupplierId,
                CompanyName = supplier.CompanyName,
                Email = supplier.Email,
                CompanyPhone = supplier.CompanyPhone,
                Mobilephone = supplier.Mobilephone,
                YahooId = supplier.YahooId,
                SkypeId = supplier.SkypeId,
                GoogleId = supplier.GoogleId,
                FacebookId = supplier.FacebookId,
                UserName = supplier.UserName,
                Password = supplier.Password,
                Address = supplier.Address,
                Description = supplier.Description,
                Status = supplier.Status,
                Version = supplier.Version
            };
        }

        /// <summary>
        /// Transfers list of Traffic BOs to list of Traffic DTOs.
        /// </summary>
        /// <param name="Traffic">List of Traffic BOs.</param>
        /// <returns>List of Traffic DTOs.</returns>
        public static TrafficDto[] ToDataTransferObjects(IEnumerable<Traffic> trafficS)
        {
            if (trafficS == null) return null;
            return trafficS.Select(t => ToDataTransferObject(t)).ToArray();
        }

        /// <summary>
        /// Transfers Traffic BO to Traffic DTO.
        /// </summary>
        /// <param name="Traffic">Traffic BO.</param>
        /// <returns>Traffic DTO.</returns>
        public static TrafficDto ToDataTransferObject(Traffic traffic)
        {
            if (traffic == null) return null;

            return new TrafficDto
            {
                TrafficId = traffic.TrafficId,
                TrafficName = traffic.TrafficName,
                SupplierId = traffic.SupplierId,
                ServiceId = traffic.ServiceId,
                TrafficTypeId = traffic.TrafficTypeId,
                DriverId = traffic.DriverId,
                SubDriverId1 = traffic.SubDriverId1,
                SubDriverId2 = traffic.SubDriverId2,
                Layer = traffic.Layer,
                L1RowNumber = traffic.L1RowNumber,
                L1NCPR = traffic.L1NCPR,
                L2RowNumber = traffic.L2RowNumber,
                L2NCPR = traffic.L2NCPR,
                Description = traffic.Description,
                Picture = traffic.Picture,
                Version = traffic.Version,
                Status = traffic.Status,
               };
        }

        public static Traffic FromDataTransferObject(TrafficDto traffic)
        {
            if (traffic == null) return null;

            return new Traffic
            {
                TrafficId = traffic.TrafficId,
                TrafficName = traffic.TrafficName,
                SupplierId = traffic.SupplierId,
                ServiceId = traffic.ServiceId,
                TrafficTypeId = traffic.TrafficTypeId,
                DriverId = traffic.DriverId,
                SubDriverId1 = traffic.SubDriverId1,
                SubDriverId2 = traffic.SubDriverId2,
                Layer = traffic.Layer,
                L1RowNumber = traffic.L1RowNumber,
                L1NCPR = traffic.L1NCPR,
                L2RowNumber = traffic.L2RowNumber,
                L2NCPR = traffic.L2NCPR,
                Description = traffic.Description,
                Picture = traffic.Picture,
                Version = traffic.Version,
                Status = traffic.Status,
            };
        }
        /// <summary>
        /// Transfers list of TrafficType BOs to list of TrafficType DTOs.
        /// </summary>
        /// <param name="TrafficType">List of TrafficType BOs.</param>
        /// <returns>List of TrafficType DTOs.</returns>
        public static TrafficTypeDto[] ToDataTransferObjects(IEnumerable<TrafficType> trafficTypes)
        {
            if (trafficTypes == null) return null;
            return trafficTypes.Select(t => ToDataTransferObject(t)).ToArray();
        }

        /// <summary>
        /// Transfers TrafficType BO to TrafficType DTO.
        /// </summary>
        /// <param name="TrafficType">TrafficType BO.</param>
        /// <returns>TrafficType DTO.</returns>
        public static TrafficTypeDto ToDataTransferObject(TrafficType trafficType)
        {
            if (trafficType == null) return null;

            return new TrafficTypeDto
            {
                TrafficTypeId = trafficType.TrafficTypeId,
                Description = trafficType.Description,                                
            };
        }

        public static TrafficType FromDataTransferObject(TrafficTypeDto trafficType)
        {
            if (trafficType == null) return null;

            return new TrafficType
            {
                TrafficTypeId = trafficType.TrafficTypeId,
                Description = trafficType.Description,
            };
        }

        /// <summary>
        /// Transfers a Shopping Cart BO to Shopping Cart DTO.
        /// </summary>
        /// <param name="cart">Shopping Cart BO.</param>
        /// <returns>Shopping Cart DTO.</returns>
        public static ShoppingCartDto ToDataTransferObject(ShoppingCart cart)
        {
            if (cart == null) return null;

            return new ShoppingCartDto
            {
                Shipping = cart.Shipping,
                SubTotal = cart.SubTotal,
                Total = cart.Total,
                ShippingMethod = cart.ShippingMethod.ToString(),
                CartItems = ToDataTransferObject(cart.Items)
            };
        }

        /// <summary>
        /// Transfers list of Shopping Cart Item BOs to list of Shopping Cart Items DTOs.
        /// </summary>
        /// <param name="cartItems">List of Shopping Cart Items BOs.</param>
        /// <returns>List of Shopping Cart Items DTOs.</returns>
        private static ShoppingCartItemDto[] ToDataTransferObject(IList<ShoppingCartItem> cartItems)
        {
            if (cartItems == null) return null;
            return cartItems.Select(i => ToDataTransferObject(i)).ToArray();
        }

        /// <summary>
        /// Transfers Shopping Cart Item BO to Shopping Cart Item DTO.
        /// </summary>
        /// <param name="item">Shopping Cart Item BO.</param>
        /// <returns>Shopping Cart Item DTO.</returns>
        private static ShoppingCartItemDto ToDataTransferObject(ShoppingCartItem item)
        {
            return new ShoppingCartItemDto
            {
                Id = item.Id,
                Name = item.Name,
                UnitPrice = item.UnitPrice,
                Quantity = item.Quantity
            };
        }

        /// <summary>
        /// Transfers Customer DTO to Customer BO.
        /// </summary>
        /// <param name="c">Customer DTO.</param>
        /// <returns>Customer BO.</returns>
        //public static Customer FromDataTransferObject(CustomerDto c)
        //{
        //    if (c == null) return null;

        //    return new Customer
        //    {
        //        CustomerId = c.CustomerId,
               
        //        City = c.City,
        //        Country = c.Country,
        //        Version = c.Version,               
        //    };
        //} 


        /// <summary>
        /// Transfers Shopping Cart Item DTO to Shopping Cart Item BO.
        /// </summary>
        /// <param name="item">Shopping Cart Item DTO.</param>
        /// <returns>Shopping Cart Item BO.</returns>
        public static ShoppingCartItem FromDataTransferObject(ShoppingCartItemDto item)
        {
            return new ShoppingCartItem
            {
                Id = item.Id,
                Name = item.Name,
                Quantity = item.Quantity,
                UnitPrice = item.UnitPrice
            };
        }
    }
}
